package com.hjt.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjt.common.to.MemberPrice;
import com.hjt.common.to.ware.SkuReductionTo;
import com.hjt.common.utils.PageUtils;
import com.hjt.common.utils.Query;
import com.hjt.gulimall.coupon.dao.SkuFullReductionDao;
import com.hjt.gulimall.coupon.entity.MemberPriceEntity;
import com.hjt.gulimall.coupon.entity.SkuFullReductionEntity;
import com.hjt.gulimall.coupon.entity.SkuLadderEntity;
import com.hjt.gulimall.coupon.service.MemberPriceService;
import com.hjt.gulimall.coupon.service.SkuFullReductionService;
import com.hjt.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private MemberPriceService memberPriceService;

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private SkuFullReductionService skuFullReductionService;
    
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存满减信息
     * @param skuReductionTo
     */
    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        /*
         *  5.4 sku的优惠，满减信息 gulimall_sms -->
         *  sms_member_price(会员价格表)
         *  sms_sku_ladder（商品阶梯价格）
         *  sms_sku_full_reduction（满减表 ）
         */

        // 保存阶梯价格
        SkuLadderEntity skuLadder = new SkuLadderEntity();
        BeanUtils.copyProperties(skuReductionTo, skuLadder);
        skuLadder.setSkuId(skuReductionTo.getSkuId());
        skuLadder.setFullCount(skuReductionTo.getFullCount());
        skuLadder.setDiscount(skuReductionTo.getDiscount());
        skuLadder.setAddOther(skuReductionTo.getCountStatus());
        skuLadderService.save(skuLadder);

        // 保存满减
        SkuFullReductionEntity fullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo,fullReductionEntity);
        fullReductionEntity.setAddOther(skuReductionTo.getCountStatus());
        // 满减价大于0才保存
        if (fullReductionEntity.getFullPrice().compareTo(new BigDecimal(0)) > 0) {
            this.save(fullReductionEntity);
        }


        // 保存会员价格,过滤小于0的会员价
        List<MemberPrice> memberPriceList = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntities = memberPriceList.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setMemberLevelId(item.getId()); // 会员等级id
            memberPriceEntity.setMemberLevelName(item.getName()); // 会员等级名称
            memberPriceEntity.setAddOther(1); // 是否能叠加优惠
            return memberPriceEntity;
        }).filter(item-> item.getMemberPrice().compareTo(new BigDecimal(0))>0).collect(Collectors.toList());

        memberPriceService.saveBatch(memberPriceEntities);
        
    }
}